package com.dormms.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dormms.bean.GoodInfo;
import com.dormms.bean.GoodRepair;
import com.dormms.dto.GoodRepairInfo;
import com.dormms.dto.GoodRepairShowInfo;
import com.dormms.dto.UserRoleDto;
import com.dormms.service.GoodRepairService;
import com.dormms.service.GoodService;
import com.dormms.tool.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class GoodRepairController {
	@Autowired
	private GoodRepairService goodRepairService;
	@Autowired
	private GoodService goodService;
	
	@RequestMapping("stu/getAllGoodInfo")
	@ResponseBody
	public List<GoodInfo> getAllGoodInfo(){
		GoodInfo goodInfo = new GoodInfo();
		return goodService.queryGoodInfoByGoodInfo(goodInfo);
	}
	
	@RequestMapping("stu/insertGoodRepair")
	@ResponseBody
	public Integer insertGoodRepair(GoodRepair goodRepair, HttpSession session) {
		UserRoleDto userRoleDto = (UserRoleDto) session.getAttribute("userRoleDto");
		goodRepair.setStuId(userRoleDto.getUserId());
		return goodRepairService.insertGoodReapir(goodRepair);
	}
	
	@RequestMapping("stu/getGoodRepairShowInfoByStu")
	@ResponseBody
	public PageInfo<GoodRepairShowInfo> getGoodRepairShowInfoByStu(Page page, GoodRepair goodRepair, HttpSession session){
		UserRoleDto userRoleDto = (UserRoleDto) session.getAttribute("userRoleDto");
		goodRepair.setStuId(userRoleDto.getUserId());
		PageHelper.startPage(page.getCurrPage(), page.getPageSize());
		List<GoodRepairShowInfo> repairShowInfos = goodRepairService.getGoodRepairShowInfoByStu(goodRepair);
		PageInfo<GoodRepairShowInfo> pageInfo = new PageInfo<GoodRepairShowInfo>(repairShowInfos);
		return pageInfo;
	}
	
	@RequestMapping("stu/updateGoodRepairByStu")
	@ResponseBody
	public Integer updateGoodRepairByStu(GoodRepair goodRepair, HttpSession session) {
		UserRoleDto userRoleDto = (UserRoleDto) session.getAttribute("userRoleDto");
		goodRepair.setStuId(userRoleDto.getUserId());
		return goodRepairService.updateGoodRepairByStu(goodRepair);
	}
	
	
	@RequestMapping("stu/getGoodRepairInfo")
	@ResponseBody
	public GoodRepairInfo getGoodRepairInfo(Integer goodRepairId) {
		return goodRepairService.getGoodRepairInfo(goodRepairId);
	}
	
	@RequestMapping("stu/deleteGoodRepairInfo")
	@ResponseBody
	public Integer deleteGoodRepairInfo(Integer goodRepairId) {
		return goodRepairService.deleteGoodRepairInfo(goodRepairId);
	}
	
	@RequestMapping("staff/getGoodRepairInfo")
	@ResponseBody
	public GoodRepairInfo getGoodRepairInfoByStaff(Integer goodRepairId) {
		return goodRepairService.getGoodRepairInfo(goodRepairId);
	}
	
	@RequestMapping("staff/getGoodRepairShowInfoByStaff")
	@ResponseBody
	public PageInfo<GoodRepairShowInfo> getGoodRepairShowInfoByStaff(Page page, GoodRepair goodRepair, HttpSession session){
		UserRoleDto userRoleDto = (UserRoleDto) session.getAttribute("userRoleDto");
		goodRepair.setStaffId(userRoleDto.getUserId());
		goodRepair.setGoodId(goodService.queryGoodInfoIdIdByStaffId(userRoleDto.getUserId()));
		PageHelper.startPage(page.getCurrPage(), page.getPageSize());
		List<GoodRepairShowInfo> repairShowInfos = goodRepairService.getGoodRepairShowInfoByStaff(goodRepair);
		PageInfo<GoodRepairShowInfo> pageInfo = new PageInfo<GoodRepairShowInfo>(repairShowInfos);
		return pageInfo;
	}
	
	@RequestMapping("staff/updateGoodRepairStatusByStaff")
	@ResponseBody
	public Integer updateGoodRepairStatusByStaff(GoodRepair goodRepair, HttpSession session) {
		UserRoleDto userRoleDto = (UserRoleDto) session.getAttribute("userRoleDto");
		goodRepair.setStaffId(userRoleDto.getUserId());
		return goodRepairService.updateGoodRepairStatusByStaff(goodRepair);
	}
	
	@RequestMapping("staff/updateGoodRepairByStaff")
	@ResponseBody
	public Integer updateGoodRepairByStaff(GoodRepair goodRepair, HttpSession session) {
		goodRepair.setRepairTime(new Date());
		return goodRepairService.updateGoodRepairByStaff(goodRepair);
	}
}
